<template>
<el-form ref="Logrec" :model="Logrec" label-width="150px">
  <el-form-item required label="登录用户名">
    <el-input type="textarea" v-model="Logrec.user" style="width:50%;float: left;"></el-input>
  </el-form-item>
  <el-form-item required label="登录用户主机IP">
    <el-input required type="textarea" v-model="Logrec.ip" readonly style="width:50%;float: left;"></el-input>
  </el-form-item>
  <el-form-item required label="登录状态">
    <el-select style="float: left;" v-model="Logrec.logtype" placeholder="请选择登录状态">
      <el-option type="number" label="登录" value=1></el-option>
      <el-option label="登出" value=0></el-option>
    </el-select>
  </el-form-item>
  <el-form-item required label="时间">
    <el-col :span="11">
      <el-date-picker type="date" placeholder="选择日期" v-model="Logrec.time" style="width: 30%;float: left;"></el-date-picker>
    </el-col>
  </el-form-item>
  <el-form-item required label="地址">
    <el-input type="textarea" v-model="Logrec.address" style="width:50%;float: left;"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="add">采集记录</el-button>
    <el-button @click="goIndex">取消</el-button>
  </el-form-item>
</el-form>
</template>
<script>
import { addLog,getLocaladdress } from '@/api'
  export default {
    data() {
      return {
        Logrec: {
          time: '',
          address: '',
          type: 1,
          user: '',
          ip: '',
          logtype: null,
        }
      }
    },
    methods: {
        goIndex(){
            this.$router.push('/collectbottons')
        },
        add(){
            addLog(this.Logrec).then((res) => {
            let _this = this;
            if(res){
                _this.$alert('采集成功！', '采集数据', {
                    confirmButtonText: '确定',
                    callback: action => {
                        //跳转
                        _this.$router.push('/loglist/?id=' + _this.Logrec.logtype)
                    }
                });
            }else{
                this.$message({
                    showClose: true,
                    message: '采集失败！',
                    type: 'error'
                });
            }
            })
        }
    },
    created(){
      getLocaladdress().then((response) => {
        this.Logrec.ip = response.data.ip
        this.Logrec.address = response.data.country_name + ',' + response.data.region + ',' + response.data.city
      })
    }
  }
</script>